import { doGet, appJsonPost } from '@/network/request';

// 获取 CSRF Token
function getCsrfToken() {
    const name = 'XSRF-TOKEN=';
    const decodedCookie = decodeURIComponent(document.cookie);
    const cookies = decodedCookie.split(';');
    for (let i = 0; i < cookies.length; i++) {
        const cookie = cookies[i].trim();
        if (cookie.indexOf(name) === 0) {
            return cookie.substring(name.length, cookie.length);
        }
    }
    console.warn('CSRF Token not found in cookies');
    return '';
}
/**
 * 新增权限
 */
export function addApi(data: object) {
    return appJsonPost({
        url: "/authority/add",
        data: data,
        headers: {
            'X-XSRF-TOKEN': getCsrfToken() // 添加 CSRF Token
        }
    });
}

/**
 * 查询权限表格树
 */
export function queryTableTreeApi() {
    return doGet({
        url: "/authority/queryTableTree",
        headers: {
            'X-XSRF-TOKEN': getCsrfToken() // 添加 CSRF Token
        }
    });
}

/**
 * 查询权限树
 */
export function queryTreeApi() {
    return doGet({
        url: "/authority/queryTree",
        headers: {
            'X-XSRF-TOKEN': getCsrfToken() // 添加 CSRF Token
        }
    });
}

/**
 * 按id删除
 */
export function deleteByIdApi(id: string) {
    return doGet({
        url: "/authority/deleteById",
        params: { id },
        headers: {
            'X-XSRF-TOKEN': getCsrfToken() // 添加 CSRF Token
        }
    });
}

/**
 * 通过id进行查询
 */
export function queryByIdApi(id: string) {
    return doGet({
        url: "/authority/queryById",
        params: { id },
        headers: {
            'X-XSRF-TOKEN': getCsrfToken() // 添加 CSRF Token
        }
    });
}

/**
 * 通过id进行修改
 */
export function modifyByIdApi(data: object) {
    return appJsonPost({
        url: "/authority/modifyById",
        data: data,
        headers: {
            'X-XSRF-TOKEN': getCsrfToken() // 添加 CSRF Token
        }
    });
}

/**
 * 查询全部后端uri映射
 */
export function listBackUriApi() {
    return doGet({
        url: "/authority/listBackUri",
        headers: {
            'X-XSRF-TOKEN': getCsrfToken() // 添加 CSRF Token
        }
    });
}
